package com.jjiang.bbmall.member.service.impl;

import com.jjiang.bbmall.member.dao.MemberLevelDao;
import com.jjiang.bbmall.member.entity.MemberLevelEntity;
import com.jjiang.bbmall.member.exception.PhoneExistsException;
import com.jjiang.bbmall.member.exception.UserNameExistsException;
import com.jjiang.bbmall.member.vo.MemberLoginVo;
import com.jjiang.bbmall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjiang.common.utils.PageUtils;
import com.jjiang.common.utils.Query;

import com.jjiang.bbmall.member.dao.MemberDao;
import com.jjiang.bbmall.member.entity.MemberEntity;
import com.jjiang.bbmall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo vo) {
        MemberEntity entity = new MemberEntity();

        // 1.设置会员的默认等级
        MemberLevelEntity memberLevelEntity = memberLevelDao.getDefaultLevel();
        entity.setLevelId(memberLevelEntity.getId());

        // 检查用户名和手机号是否唯一；若不满足，则抛出异常
        checkPhoneUnique(vo.getPhone());
        checkUserNameUnique(vo.getUserName());

        entity.setMobile(vo.getPhone());
        entity.setUsername(vo.getUserName());

        // 存储密码，需要加密存储！【重点】
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encodedPassword = passwordEncoder.encode(vo.getPassword());
        entity.setPassword(encodedPassword);

        // 设置默认昵称
        entity.setNickname(vo.getUserName());
        // 插入数据库
        baseMapper.insert(entity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistsException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (count > 0) {
            throw new PhoneExistsException();
        }
    }

    @Override
    public void checkUserNameUnique(String userName) throws UserNameExistsException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (count > 0) {
            throw new UserNameExistsException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String loginAccount = vo.getLoginAccount();
        String password = vo.getPassword();

        // 1.去数据库查询账户[用户名/手机号]对应的密码
        MemberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginAccount).or().eq("mobile", loginAccount));
        if (entity == null) {
            return null;
        }
        // 2.对比密码
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean matches = passwordEncoder.matches(password, entity.getPassword());
        if (matches) {
            return entity;
        } else {
            return null;
        }
    }

}
